<?php
abstract class My_Controller extends Zend_Controller_Action
{
		public $config;
		public $params;
		public $db;
		public $sh_helpers = array();
		public $jquery_path = '/system/js/jquery/';
		public $uiTheme = 'default';
		public $_frontController;
		public $sh_language = null;

		function preDispatch()
		{
			if(Zend_Registry::isRegistered('sh_language'))
				$this->sh_language = Zend_Registry::get('sh_language');
			/** Set request params */
			$request = Zend_Controller_Front::getInstance()->getRequest();
			$this->params = $request->getParams();
			$this->SH_init();
		}

		/**
		 * My_Controller_Action::SH_init()
		 *
		 *  Controller Initialization function
		 *
		 * @return void
		 */
		public function SH_init()
		{
			/** Set active configuration */
			$this->config = Zend_Registry::get('sh_config');

			/** Setup Zend Flash Messenger */
			$this->sh_messenger = $this->_helper->getHelper('FlashMessenger');

			/** Setup language */
			//$this->setupLanguage(); moved to plugin

			/** Setup view object */
			$this->setupView();

			/** Setup Layout template */
			$this->setLayout();

			/** Setup User */
			

			/* Setup Session */
			$this->sh_session = new Zend_Session_Namespace('My');
			$this->view->sh_session = $this->sh_session;

			/** Setup ACL and check permissions*/
//			$this->sh_acl = new My_Acl();
//    	if(!$this->sh_acl->checkLevel($this->sh_user)) 
//    		$this->_redirect('denied');

      /** Setup Ajax+uiJQuery context helper */
			if(isset($this->sh_ajaxian))
			{
			    $this->AjaxContext =  $this->_helper->getHelper('AjaxContext');
			    $context_exist = $this->AjaxContext->hasContext('ajax');
			    if(!$context_exist)
			    {
	          $this->AjaxContext->addContext('ajax',
	          				array('callbacks'=>
	          						array('post' =>
	          							array('My_Controller','ajaxLayout'))));

				    foreach($this->sh_ajaxian as $method)
				       $this->AjaxContext->addActionContext($method,'ajax');
	          		$this->AjaxContext->initContext('ajax');
			    }
			}

		}

		/**
		 * My_Controller_Action::setupView()
		 *
		 * Setup view (Set template indicated by default in database table sh_templates)
		 *
		 * @return void
		 */
		public function setupView()
		{
			
			/** Dojo Setup */
      Zend_Dojo::enableView($this->view);
      $this->view->dojo()->setLocalPath('/system/dojo/dojo/dojo.js')
												 ->addStyleSheetModule('dijit.themes.tundra')
												 ->setDjConfigOption('parseOnLoad', true);
      $this->view->dojo()->enable();
      //debug(get_class_methods($this->view->dojo()));


			/** JQuery Setup */
			ZendX_JQuery::enableView($this->view);
			$this->view->jQuery()->setLocalPath($this->jquery_path.'core/jquery-1.3.1.js');

			if(in_array('jQuery',$this->sh_helpers) or in_array('uiJQuery',$this->sh_helpers))
			  $this->view->jQuery()->enable();

			$this->view->debug = $this->params;

			/** JQuery UI Setup */
		  	if(isset($this->sh_helpers['uiJQuery']['theme']))
		      $this->uiTheme = $this->sh_helpers['uiJQuery']['theme'];
		  	$this->view->jQuery()->setUiLocalPath($this->jquery_path.'core/jquery.ui.1.6.js');
			$this->view->jQuery()->addStylesheet($this->jquery_path.'themes/'.$this->uiTheme.'/ui.all.css');

			if(in_array('uiJQuery',$this->sh_helpers))
			    $this->view->jQuery()->uiEnable();

			if(isset($this->sh_helpers['JQueryPlugins']) and is_array($this->sh_helpers['JQueryPlugins']))
			{
			    foreach($this->sh_helpers['JQueryPlugins'] as $folder=>$plugin)
			        $this->view->headScript()->appendFile($this->jquery_path.'plugins/'.$folder.'/'.$plugin,'text/javascript');
			}

			if(isset($this->params['dialogID'])){
			    $this->view->assign('sh_dialogID',$this->params['dialogID']);
			}

			$this->view->assign('sh_pathInfo',$this->getRequest()->getPathInfo());

			$this->view->assign('sh_language',$this->sh_language);

		}

		/**
		 * Ajax content callback function
		 *
		 */
		public function ajaxLayout()
		{
		    $config = Zend_Registry::get('sh_config');
		    $layout = Zend_Layout::getMvcInstance();
		    $layout->setLayoutPath($config->path->layouts.'system');
		    $layout->setLayout('ajax');
		}

		public function imageLayout()
		{
		    $config = Zend_Registry::get('config');
		    $layout = Zend_Layout::getMvcInstance();
		    $layout->setLayoutPath($config->path->layouts.'system');
		    $layout->setLayout('image');
		    $this->getHelper('viewRenderer')->setNoRender();
		}

		public function setLayout($admin = false)
		{
//
//		  $front = 'front';
//		  if($admin) $front = 'back';
//		  
//		  /** Setup Application Layout Path */
//		  $templateModel = new Template_Model_Template();
//			
//		  $current_template = $templateModel->fetchRow("{$front} = 1");
//		  $layout = $this->_helper->getHelper('layout'); 
//		  $layout->setLayoutPath($this->config->path->layouts.$current_template->name);
//		  
//		  /** Setup Application Layout Name */
//			$layoutModel = new Template_Model_Layout();
//			$where = array(
//								"module = '{$this->params['module']}'",
//								"controller = '{$this->params['controller']}'",
//								"action = '{$this->params['action']}'",
//								"template = {$current_template->id}"
//								);			
//		  $current_layout = $layoutModel->fetchRow($where);				 
//		  if(!empty($current_layout))
//		  	$layout->layout->setLayout($current_layout->name);
//
//      $this->view->assign('sh_template',$current_template);
      
		}

		public function getParam($name)
		{
		    if(isset($this->params[$name]))
		        return $this->params[$name];
		    else
		        return NULL;
		}


}